package shopping.map;

import shopping.entity.User;

import java.util.*;

/**
 * @description
 */
public class TestMap {
    public static void main(String[] args) {
        //集合泛型化
        Map<String, User> userMap = new HashMap<String, User>();

        //创建三个用户
        User u1 = new User("u00001", "赵子锐", "123456", "管理员", '男', "大同");
        User u2 = new User("u00002", "张三", "123456", "管理员", '女', "太原");
        User u3 = new User("u00003", "李四", "123456", "管理员", '男', "北京");

        //向集合内添加
        userMap.put("u00001", u1);
        userMap.put("u00002", u2);
        userMap.put("u00003", u3);
        userMap.put("u00001", u1);//重复的
        userMap.put(null, null);//空对象也可以正常添加进去

        //遍历集合

        // 遍历集合的所有key          //userMap.keySet()拿到所有的key
        System.out.println("遍历集合所有的key：");
        Iterator<String> iterator1 = userMap.keySet().iterator();
        while (iterator1.hasNext()) {
            System.out.println(iterator1.next());
        }

        System.out.println("==============================");

        //遍历集合的所有value
        System.out.println("集合中所有的value：");
        Iterator<User> iterator2 = userMap.values().iterator();
        while (iterator2.hasNext()) {
            System.out.println(iterator2.next());
        }

        System.out.println("===============================");

        //遍历集合所有的key和value
        System.out.println("所有的key和value");
        Iterator<Map.Entry<String, User>> iterator3 = userMap.entrySet().iterator();
        while (iterator3.hasNext()) {
            Map.Entry<String, User> entry = iterator3.next();
            System.out.print("用户id为");
            System.out.println(entry.getKey());

            System.out.print("用户value为");
            System.out.println(entry.getValue());
        }
    }
}
